package com.lin.subject.infra.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.subject.infra.basic.mapper.SubjectCategoryDao;
import com.lin.subject.infra.basic.entity.SubjectCategory;
import com.lin.subject.infra.basic.service.SubjectCategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 题目分类(SubjectCategory)表服务实现类
 *
 * @author makejava
 * @since 2024-10-19 17:22:26
 */
@Service("subjectCategoryService")
public class SubjectCategoryServiceImpl extends ServiceImpl<SubjectCategoryDao, SubjectCategory> implements SubjectCategoryService {

    @Resource
    private SubjectCategoryDao subjectCategoryDao;
    @Override
    public int insert(SubjectCategory subjectCategory) {
        return subjectCategoryDao.insert(subjectCategory);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SubjectCategory queryById(Long id) {
        return subjectCategoryDao.selectById(id);
    }

    /**
     * 修改数据
     *
     * @param subjectCategory 实例对象
     * @return 实例对象
     */
    @Override
    public int update(SubjectCategory subjectCategory) {
        return subjectCategoryDao.updateById(subjectCategory);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Long id) {
        return subjectCategoryDao.deleteById(id);
    }

    /**
     * 查询岗位大类
     *
     * @param subjectCategory
     */
    @Override
    public List<SubjectCategory> queryCategory(SubjectCategory subjectCategory) {
        LambdaQueryWrapper<SubjectCategory> queryWrapper = new LambdaQueryWrapper<>();

        //父id条件
        queryWrapper.eq(Objects.nonNull(subjectCategory.getParentId()),SubjectCategory::getParentId, subjectCategory.getParentId());

        // 名字
        queryWrapper.like(Objects.nonNull(subjectCategory.getCategoryName()),SubjectCategory::getCategoryName, subjectCategory.getCategoryName());

        // 分类类型
        queryWrapper.eq(Objects.nonNull(subjectCategory.getCategoryType()),SubjectCategory::getCategoryType, subjectCategory.getCategoryType());

        // 删除状态
        queryWrapper.eq(SubjectCategory::getIsDeleted, subjectCategory.getIsDeleted());

        return subjectCategoryDao.selectList(queryWrapper);

    }

    /**
     * 查询每个分类的题目数量
     *
     * @param id
     * @return
     */
    @Override
    public Integer querySubjectCount(Long id) {
        return lambdaQuery().eq(SubjectCategory::getId, id).count();
    }

    /**
     * 查询每个分类对应的题目数量
     *
     * @return
     */
    @Override
    public List<SubjectCategory> querySubjectTypeCount() {
        QueryWrapper<SubjectCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("category_type","count(*) as count");
        queryWrapper.groupBy("category_type");
        return subjectCategoryDao.selectList(queryWrapper);
    }

    @Override
    public List<SubjectCategory> selectByParentId(Long id) {
        return lambdaQuery().eq(SubjectCategory::getParentId,id).list();
    }
}

